package com.mc.metaSearch;

import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class SearchNews {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(SearchNews.class);

	final String prefix = "http://news.baidu.com/ns?cl=2&rn=30&tn=news&word=";
	public String[] getContent(String querystring) throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append(prefix).append(querystring);// "GBK"
		//logger.info(sb + "---url");
		URL l = new URL(sb.toString());
		// l.openConnection();
		HttpURLConnection con = (HttpURLConnection) l.openConnection();
		con.setConnectTimeout(10000);
		con.setReadTimeout(18000);
		BufferedReader r = new BufferedReader(new InputStreamReader(
				l.openStream(), "utf-8"));// "GBK"
		StringBuffer buf = new StringBuffer();
		/*
		 * System.setProperty("sun.net.client.defaultConnectTimeout", "15000");
		 * System.setProperty("sun.net.client.defaultReadTimeout", "15000");
		 */
		Pattern p = Pattern
				.compile("<ul>.*</ul>");
		List<String> result = new ArrayList<String>();

		String str = null;
		while ((str = r.readLine()) != null) {
			buf.append(str);
			str = null;
		}
		//logger.info(buf.toString());
		Matcher m = p.matcher(buf);
		boolean test = m.find();
       // logger.info(test+"--------find");
		if (test) {
			//logger.info("---testfind");
			String[] src = m.group().split("</li>");
			//logger.info(src.length+"---legth");
			for (String i : src) {
				//logger.info(i + "----conten");
				String content = i.replaceAll("</td></tr></table><br>.*", "");
				if (!(content.isEmpty() || content.equals(""))) {
					result.add(content);
				}
			}
		}
		return (String[]) result.toArray(new String[result.size()]);
	}

	public Map<Integer, String> getURL(String[] content) throws Exception {
		Pattern p = Pattern
				.compile("<h3 class=\"c-title\"><a href=.*");
		Map<Integer, String> urls = new HashMap<Integer, String>();
		int k = 0;
		for (String i : content) {

			Matcher m = p.matcher(i);
			if (m.find()) {
				String[] src = m.group().split("<h3 class=\"c-title\"><a href=\"");

				for (String url : src) {
					//logger.info(url +"--------url");
					String url1 = url.replaceAll("\"    data-click=\".*", "");
					if (!(url1.isEmpty() || url1.equals("")))
					// urls.add(url1);
					{
					//	logger.info(url1 + "------url1");
						urls.put(k, url1);
						// break;
					}

				}
			}
			k++;
		}
		return (urls);

	}

	public Map<Integer, String> getTitle(String[] content) throws Exception {
		Pattern p_title = Pattern.compile("target=\"_blank\".*</a></h3>");
		Map<Integer, String> titles = new HashMap<Integer, String>();
		int k = 0;
		for (String i : content) {
			Matcher m = p_title.matcher(i);
			boolean test = m.find();
	       // logger.info(test+"--------find");
			if (test) {
				String[] src = m.group().split("</a></h3>");
            
				for (String title1 : src) {
				//	logger.info(title1 + "-------------title");
					String title = title1.replaceAll("target=\"_blank\"                >", "")
							.replaceAll("<[^>]*>", "")
							.replaceAll("[(/>)<]", "")
							.replaceAll("<span", "")
							;
              
					if (!(title.isEmpty() || title.equals(""))) {
					//	logger.info(title + "---------title1");
						titles.put(k, title);
						// break;
					}
				}
			}
			k++;
		}

		return (titles);
	}

	public Map<Integer, String> getAbstract(String[] content) throws Exception {

		Pattern p_abstract = Pattern.compile("<div class=\"c-summary\">.*target=\"_blank\"  class=\"c-cache\">");
		Map<Integer, String> abstracts = new HashMap<Integer, String>();
		int k = 0;
		for (String i : content) {
			//logger.info(i);
			Matcher m = p_abstract.matcher(i);
			boolean test = m.find();
		       // logger.info(test+"--------find");
				if (test) {
				String[] src = m.group().split("&nbsp;<a href=\"");

				//for (String abstract1 : src) {
				//	logger.info(abstract1 + "----abs1");
					String abstract_ = src[0]
							.replaceAll("<div class=\"c-summary\" ", "")
							.replaceAll("<[^>]*>", "")
							.replaceAll("[(/>)<]", "")
							.replaceAll("&nbsp;", " ");
					;
					if (!(abstract_.isEmpty() || abstract_.equals("")))
						//logger.info(abstract_ + "--------------abs1111111111111111");
						abstracts.put(k, abstract_);
				//}
			}
			k++;
		}
		return (abstracts);
	}

	public static void main(String[] args) {
		SearchNews b = new SearchNews();
		String[] r;
		Map<Integer, String> urls;
		Map<Integer, String> abstracts;
		Map<Integer, String> titles;
		try {
			r = b.getContent("����ʮ��");
			urls = b.getURL(r);
			abstracts = b.getAbstract(r);
			titles = b.getTitle(r);
			Iterator it=abstracts.keySet().iterator();
			while(it.hasNext()){
				Object key=it.next();
				Object value=abstracts.get(key);
				System.out.println(value.toString());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
